package com.producer.model;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.http.HttpStatus;
import xiaohua.utils.model.FieldsDto;

import java.io.Serializable;
import java.util.List;

/**
 * @author liulanhua
 * @date
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 是否成功
     */
    @ApiModelProperty(name = "success", value = "是否成功", example = "true")
    private boolean success;

    /**
     * 返回码
     */
    @ApiModelProperty(name = "code", value = "返回码", example = "200")
    private Integer code;

    /**
     * 返回消息
     */
    @ApiModelProperty(name = "msg", value = "返回消息", example = "返回消息")
    private String msg;

    /**
     * 返回数据
     */
    @ApiModelProperty(name = "data", value = "返回的数据")
    private T data;

    /**
     * 返回数据体字段
     */
    @ApiModelProperty(name = "entityFields", value = "返回数据体字段", example = "[{\"menuName\":\"menuName1\",\"property\":\"property1\"},{\"menuName\":\"menuName2\",\"property\":\"property2\"},{\"menuName\":\"menuName3\",\"property\":\"property3\"}]")
    private List<FieldsDto> entityFields;

    public Result() {
        super();
        this.success = Boolean.FALSE;
        this.data = null;
        this.msg = null;
        this.code = HttpStatus.BAD_REQUEST.value();
    }

    public Result(boolean success) {
        super();
        this.success = success;
        this.data = null;
        this.msg = null;
        if (success) {
            this.code = HttpStatus.OK.value();
        }
    }

    public Result(boolean success, T data) {
        super();
        this.success = success;
        this.data = data;
        this.msg = null;
        if (success) {
            this.code = HttpStatus.OK.value();
        }
    }

    public Result(boolean success, T data, String msg) {
        super();
        this.success = success;
        this.data = data;
        this.msg = msg;
        if (success) {
            this.code = HttpStatus.OK.value();
        }
    }

    public Result(boolean success, Integer code, T data, String msg) {
        super();
        this.success = success;
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    /**
     * 初始失败方法
     *
     * @see [类、类#方法、类#成员]
     */
    public Result<T> invokeFail() {
        this.success = Boolean.FALSE;
        this.data = null;
        this.code = HttpStatus.BAD_REQUEST.value();
        //this.code = OperationResult.DALError.getCode();
        this.msg = "操作失败";
        return this;
    }

    public Result<T> invokeFail(String msg) {
        this.success = Boolean.FALSE;
        this.data = null;
        this.code = HttpStatus.BAD_REQUEST.value();
        //this.code = OperationResult.DALError.getCode();
        this.msg = msg;
        return this;
    }

    public Result<T> invokeFail(Integer code, String msg) {
        this.success = Boolean.FALSE;
        this.data = null;
        this.code = code;
        this.msg = msg;
        return this;
    }

    public Result<T> invokeFail(T data, Integer code, String msg) {
        this.success = Boolean.FALSE;
        this.data = data;
        this.code = code;
        this.msg = msg;
        return this;
    }

    public Result<T> invokeSuccess() {
        this.success = Boolean.TRUE;
        this.code = HttpStatus.OK.value();
        //this.code = OperationResult.Success.getCode();
        this.msg = "操作成功";
        return this;
    }

    public Result<T> invokeSuccess(T data) {
        this.success = Boolean.TRUE;
        this.data = data;
        this.msg = "操作成功";
        this.code = HttpStatus.OK.value();
        //this.code = OperationResult.Success.getCode();
        return this;
    }

    public Result<T> invokeSuccess(T data, String msg) {
        this.success = Boolean.TRUE;
        this.data = data;
        this.msg = msg;
        this.code = HttpStatus.OK.value();
        return this;
    }

    public Result<T> invokeSuccess(T data, Integer code, String msg) {
        this.success = Boolean.TRUE;
        this.data = data;
        this.msg = msg;
        this.code = code;
        return this;
    }

    /**
     * 初始失败方法
     *
     * @see [类、类#方法、类#成员]
     */
    public static <T> Result<T> invokeFailNew() {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.FALSE);
        result.setData(null);
        result.setCode(HttpStatus.BAD_REQUEST.value());
        result.setMsg("操作失败");
        return result;
    }

    public static <T> Result<T> invokeFailNew(String msg) {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.FALSE);
        result.setData(null);
        result.setCode(HttpStatus.BAD_REQUEST.value());
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> invokeFailNew(Integer code, String msg) {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.FALSE);
        result.setData(null);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> invokeFailNew(T data, Integer code, String msg) {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.FALSE);
        result.setData(data);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> invokeSuccessNew() {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.TRUE);
        result.setCode(HttpStatus.OK.value());
        result.setMsg("操作成功");
        return result;
    }

    public static <T> Result<T> invokeSuccessNew(T data) {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.TRUE);
        result.setData(data);
        result.setCode(HttpStatus.OK.value());
        result.setMsg("操作成功");
        return result;
    }

    public static <T> Result<T> invokeSuccessNew(T data, String msg) {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.TRUE);
        result.setData(data);
        result.setCode(HttpStatus.OK.value());
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> invokeSuccessNew(T data, Integer code, String msg) {
        Result<T> result = new Result<>();
        result.setSuccess(Boolean.TRUE);
        result.setData(data);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

}
